package com.buaa.aimall.goodsModule.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.buaa.aimall.Utils.PageReturnResult;
import com.buaa.aimall.goodsModule.Entity.CouponEntity;
import com.buaa.aimall.goodsModule.Enum.GoodsTypeEnum;
import com.buaa.aimall.goodsModule.Mapper.CouponMapper;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponEntity> implements CouponService {

    @Override
    public long getCountByUser(String userId) {
        LambdaQueryWrapper<CouponEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CouponEntity::getUser, userId);
        queryWrapper.ge(CouponEntity::getExpirationTime, LocalDateTime.now());
        queryWrapper.ne(CouponEntity::getUsageTimes, 0);
        return count(queryWrapper);
    }

    @Override
    public PageReturnResult<CouponEntity> getPageByUser(String userId, int pageSize, int currentPage, GoodsTypeEnum usefor) {

        LambdaQueryWrapper<CouponEntity> queryWrapper = new LambdaQueryWrapper<CouponEntity>();
        queryWrapper.eq(CouponEntity::getUser, userId);
        queryWrapper.ge(CouponEntity::getExpirationTime, LocalDateTime.now());
        queryWrapper.ne(CouponEntity::getUsageTimes, 0);
        if (usefor != null) {
            queryWrapper.eq(CouponEntity::getUsefor, usefor);
        }

        PageReturnResult<CouponEntity> result = new PageReturnResult<CouponEntity>();
        result.setTotalCount(this.count(queryWrapper));

        result.setRecords(this.page(new Page<CouponEntity>(currentPage, pageSize), queryWrapper).getRecords());
        return result;
    }

}
